package org.magicsoft.office.excel.impt;

import java.io.IOException;
import java.io.Writer;

import org.magicsoft.office.excel.model.ResultSummary;
import org.magicsoft.office.exception.OfficeException;

import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;

public class HTMLReportGenerator {

	public void generate(ResultSummary resultSummary, Writer writer) {
		try {
			this.getConfiguration().getTemplate("/report.ftl").process(resultSummary, writer);
		} catch (TemplateException | IOException e) {
			throw new OfficeException(e);
		}
	}

	private Configuration getConfiguration() {
		Configuration configuration = new Configuration(Configuration.getVersion());
		configuration.setDefaultEncoding("utf-8"); // 字符集。
		configuration.setLocalizedLookup(false); // 加载模板时忽略文件名中的国际化后缀。
		configuration.setClassicCompatible(true);
		configuration.setTemplateLoader(new ClassTemplateLoader(HTMLReportGenerator.class, "/template"));
		configuration.setNumberFormat("#");
		return configuration;
	}

}
